package com.situ.yB2C.sequnce.service.impl;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.situ.yB2C.sequnce.dao.SequnceDao;
import com.situ.yB2C.sequnce.pojo.Sequnce;
import com.situ.yB2C.sequnce.service.SequnceService;

@Service
public class SequnceServiceImpl implements SequnceService {
	private static final DateFormat FORMAT_YYMMDD = new SimpleDateFormat("yyMMdd");
	private static final DecimalFormat FORMAT_000 = new DecimalFormat("000");

	@Autowired
	private SequnceDao sequnceDao;

	@Override
	@PostConstruct
	public void initData() {
		List<Sequnce> dataList = sequnceDao.selectList(Wrappers.emptyWrapper());
		if (dataList == null || dataList.size() <= 0) {
			Sequnce sequnce = new Sequnce();
			sequnce.setColumn1(0);
			sequnce.setColumn2(0);
			sequnce.setColumn3(0);
			sequnce.setColumn4(0);
			sequnce.setActiveFlag(1);
			sequnce.setCreateBy("SYS");
			sequnce.setCreateDate(new Date());

			sequnceDao.insert(sequnce);
		}

	}

	/**
	 * .订单编号: yyMMdd001
	 * 
	 */
	@Override
	public String buildOrderNo() {
		Integer number = sequnceDao.selectColumn(Sequnce.ORDER_COLUMN_NAME) + 1;
		StringBuilder builder = new StringBuilder();
		builder.append(FORMAT_YYMMDD.format(new Date())).append(FORMAT_000.format(number));
		//sequnceDao.insertColumn(Sequnce.ORDER_COLUMN_NAME, number);
		sequnceDao.updateColumn(Sequnce.ORDER_COLUMN_NAME, number);
		
		return builder.toString();
	
		
	}

}
